<?php
include ("simple_html_dom.php");
function ak_decode($str)
{
	$str= str_replace(array("&nbsp;","<br>"),"",$str);
	$html = str_get_html($str);
	$paxname =array();
	$mangxx=array();
	$mangxx['pnr']=TrimStr($html->find('span[id=OptionalHeaderContent_lblBookingNumber]',0)->plaintext);
	foreach($html->find('div.guest-detail-caption') as $element)
				$paxname[]=str_replace("+ INFANT","",TrimStr(strtoupper(TrimStr($element->find('span', 1)->plaintext))));
//	$giave =TrimStr(str_replace(",","",$html->find('td[class=grdChrInfo]', 0)->innertext));
	$cb = array();
	$dem=0;
	foreach($html->find('table.rgMasterTable tr[id^=ctl00_OptionalHeaderContent]') as $element)
		{	
			
			  $cb[$dem]['sohieu']		=	$element->find('div.left', 0)->plaintext;//substr(TrimStr($element->find('td', 0)->innertext),0,10);
				$c=xyly_ngaybay($element->find('div[class^=itineraryCustom]', 1));
				$cb[$dem]['ngaybay']	=	$c['ngaybay'];
				$cb[$dem]['sbdi']			=	$c['sb'];
				$c=xyly_ngaybay($element->find('div[class^=itineraryCustom]', 2));
				$cb[$dem]['sbden']		=	$c['sb'];
			$dem++;
		}
	$mangxx['paxname']=$paxname;
	$mangxx['flight']=$cb;
	$mangxx['amount']=intval($html->find('input[id="xmloutput_XTOTAL"]',0)->value);;	
	return $mangxx;
}
function xyly_ngaybay($xx)
{
	$temxx=array();
	$xx->find('div', 0)->innertext='';
	$xc =TrimStr($xx->plaintext);
	$gb=substr($xc,strpos($xc,",")+2,4);
	$gb =substr($gb,0,-2).":".substr($gb,-2);
	$temxx['ngaybay']=date("d/m/Y",strtotime(substr($xc,strpos($xc,")")+2,11)));
	$temxx['sb']=$gb."&nbsp;".TrimStr(substr($xc,strpos($xc,"(")+1,3));
	return $temxx;
}

function TrimStr($str)
{
    $str        = trim($str);
    $ret_str    = "";
    for($i=0;$i < strlen($str);$i++)
    {

        if(substr($str, $i, 1) != " ")
        {

            $ret_str .= trim(substr($str, $i, 1));

        }
        else
        {
            while(substr($str,$i,1) == " ")

            {
                $i++;
            }
            $ret_str.= " ";
            $i--; // ***
        }
    }
    return $ret_str;
}